package com.itita.weibo.ui;

import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.NetUtil;
import com.itita.weibo.util.WeiboUtil;

public final class LoginActivity extends Activity implements IWeiboActivity {
	
	public ProgressDialog pd;
	
	public static final int REF_LOGIN_RESULT = 1;

	public void init() {
		// 检查网络
		if (WeiboUtil.checkNet(this)) {// 联网正常
			Log.d(" 检查网络", "网络正常");
			if (!MainService.isrun) {
				MainService.isrun = true;
				Intent it = new Intent(this, MainService.class);
				this.startService(it);
			}
			
		}else { // 提示用户网络异常
			Log.d("检查网络", "网络异常");
			MainService.alertNetError(this);
		}
	}
	protected void onResume() {
		super.onResume();
		init();
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//取消标题栏
		WeiboUtil.closeWindowTitle(this);
		
		MainService.addActivity(this);
		
		this.setContentView(R.layout.login);
		
		this.initControlsAndAddControlsEvent();
	}

	public void initControlsAndAddControlsEvent() {

		final AutoCompleteTextView acUser = (AutoCompleteTextView) findViewById(R.id.user);
		final EditText etPass = (EditText) findViewById(R.id.password);
		final Button btLogin = (Button) this.findViewById(R.id.loginButton);
//		final TextView tvTitle = (TextView) this.findViewById(R.id.textView);
		 this.findViewById(R.id.title).setVisibility(View.GONE);
//		
//		this.findViewById(R.id.title_bt_left).setVisibility(View.GONE);
//		this.findViewById(R.id.title_bt_right).setVisibility(View.GONE);
		// 设定TextView的标题文本
	
//		tvTitle.setText(this.getResources().getString(R.string.app_name));
//		tvTitle.setText(null);
//		tvTitle.setVisibility(View.GONE);
		Button restButton = (Button) this.findViewById(R.id.restButton);
		restButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				acUser.setText(null);
				etPass.setText(null);
				acUser.setFocusable(true);
			}
		});
		btLogin.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AutoCompleteTextView acUser = (AutoCompleteTextView) findViewById(R.id.user);
				EditText etPass = (EditText) findViewById(R.id.password);
//				
//				acUser.setText("jiaomingdcr@126.com");
//				etPass.setText("dcrjiaoming");
				if(!WeiboUtil.strIsNotEmpty(acUser.getText().toString())){
					WeiboUtil.myToastShow(LoginActivity.this, "请输入登陆账号");
				    return;
				}
				
				if(!WeiboUtil.strIsNotEmpty(etPass.getText().toString())){
					WeiboUtil.myToastShow(LoginActivity.this, "请输入登陆密码");
					return;
				}
				
				// 定义登录验证任务
				final HashMap param = new HashMap();
				param.put("user", acUser.getText().toString());
				param.put("pass", etPass.getText().toString());
				final Task loginTask = new Task(Task.TASK_USER_LOGIN, param);
				MainService.addNewTask(loginTask);
				// 显示进度条
				pd = new ProgressDialog(LoginActivity.this);
				pd.setMessage("正在登陆...");
				pd.setTitle(LoginActivity.this.getResources().getString(
						R.string.app_name));
				pd.show();
			}
		});
	}
	public void refresh(Object... param) {
		pd.dismiss();// 进度条消失

		Toast.makeText(this, (String) param[1], 400).show();
		if (((String) param[1]).indexOf("成功") >= 0) {
			final Intent it = new Intent(this, HomeActivity.class);
			this.startActivity(it);
			finish();
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.d("keydown", ".................." + keyCode);
		if (keyCode == 4)// 如果用户按下了返回键
		{
			MainService.promptExitApp(this);
		}
		return true;
	}

}
